package com.example.recycleview;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

public class MyService2 extends Service {
    private List<MediaPlayer> mediaPlayerList = new ArrayList<MediaPlayer>();
    private int[] musics={R.raw.music,R.raw.music1,R.raw.music2};

    public MyService2() {
        Log.d("service2","service2 is MyService2");
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.d("service2","service2 is onBind");
        return new MyBinder();
    }

    protected class MyBinder extends Binder {
        public void play(){
                MediaPlayer mediaPlayer;
                mediaPlayer = MediaPlayer.create(getApplicationContext(), musics[2]);
                mediaPlayerList.add(mediaPlayer);
                mediaPlayer.start();
        }
    }
    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("service2","service2 is onUnbind");

        return super.onUnbind(intent);
    }

    public void onDestroy() {
        super.onDestroy();
        for(MediaPlayer m:mediaPlayerList){
            m.stop();
        }
        Log.d("service2","service2 is onDestroy");
    }
}